package com.wtwd.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.device.constant.AppMsgEnum;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.dto.ReplyAddFriendDto;
import com.wtwd.device.model.entity.*;
import com.wtwd.device.model.vo.FriendHealthFileVo;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.model.vo.ResultVO;
import com.wtwd.device.service.AppUserInfoService;
import com.wtwd.device.service.AppUserService;
import com.wtwd.device.service.FriendRecordService;
import com.wtwd.device.service.FriendRequestsService;
import com.wtwd.device.util.DateUtils;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author mjy
 * @date 2021/09/16
 * @description
 */
@Api(tags = "亲友空间")
@RestController
@RequestMapping("/app-friend")
public class FriendController {


    private static final Logger logger = LoggerFactory.getLogger(FriendController.class);

    /**
     * 一天的时间戳
     */
    private Long oneTime = 86400000L;

    @Autowired
    private FriendRequestsService friendRequestsService;

    @Autowired
    private FriendRecordService friendRecordService;

    @Resource
    private AppUserInfoService appUserInfoService;

    @Resource
    private AppUserService appUserService;


    /**
     * 获取好友列表(并获取昨日好友总步数)
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取好友列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "userId", value = "页面大小", required = true, dataType = "Long")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "参数错误")
    })
    @GetMapping("/getFriendList")
    public RespEntity<FriendVo> getFriendList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestParam("userId") Long userId) {
        logger.info("getFriendList param", currentPage, pageSize, userId);
        List<FriendVo> friendVos = friendRecordService.getFriendList(userId, currentPage, pageSize);
        return new RespEntity(RespCode.SUCCESS, friendVos);
    }


    /**
     * 分页获取好友申请列表
     * 只显示申请状态中的数据
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @return
     */
    @ApiOperation(value = "分页获取好友申请列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "userId", value = "页面大小", required = true, dataTypeClass = Long.class)
    })
    @ApiResponses({@ApiResponse(code = 400, message = "参数错误")
    })
    @GetMapping("/getFriendRequestsList")
    public RespEntity<FriendRequestsVo> getFriendRequestsList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestParam("userId") Long userId) {
        logger.info("getFriendRequestsList param", currentPage, pageSize, userId);
        return friendRequestsService.getFriendRequestsList(currentPage, pageSize, userId);
    }

    /**
     * 获取申请人申请列表(只不显示同意申请的记录)
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取申请人申请列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "userId", value = "页面大小", required = true, dataTypeClass = Long.class)
    })
    @ApiResponses({@ApiResponse(code = 400, message = "参数错误")
    })
    @GetMapping("/getUserRequests")
    public RespEntity<UserRequestsVo> getUserRequests(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestParam("userId") Long userId) {
        logger.info("getUserRequests param", currentPage, pageSize, userId);
        return friendRequestsService.getUserRequestsList(currentPage, pageSize, userId);
    }


    /**
     * 添加好友
     *
     * @param friendRequests
     * @return
     */
    @ApiOperation(value = "添加好友")
    @PostMapping("/addFriend")
    public ResultVO addFriend(@RequestBody FriendRequests friendRequests) {
        logger.info("addFriend param" + friendRequests);
        ResultVO resultVO = friendRecordService.checkFriendRequests(friendRequests);
        //获取用户名称，用于推送
        Map userMap = friendRecordService.getPushUserMap(friendRequests.getRespondent());
        if (userMap == null) {
            return ResultVO.error(RespCode.UNINITIALIZED_APP_USER_INFO);
        }
        String name = (String) userMap.get("name");
        String clientType = (String) userMap.get("clientType");
        if (resultVO.getCode() != RespCode.SUCCESS.getCode()) {
            if (resultVO.getCode() == RespCode.FRIEND_APPLY_STATE.getCode()) {//处于申请状态(依旧推送申请通知,但申请记录只会产生一条)
                /*ppUserInfoService.pushTemplateMsgToAppUser(friendRequests.getRespondent(), clientType, AppMsgEnum.ADD_FRIEND);*/
                return ResultVO.success();
            }
            return resultVO;
        }
        Integer rest = friendRequestsService.insert(friendRequests);
        if (rest > 0) {
//            appUserInfoService.pushMsgToAppUser(friendRequests.getRespondent(), clientType, AppMsgEnum.ADD_FRIEND);
            return ResultVO.success();
        } else {
            return ResultVO.error();
        }
    }


    /**
     * 添加好友回复
     *
     * @param replyAddFriendDto
     * @return
     */
    @ApiOperation(value = "添加好友回复")
    @PostMapping("/replyAddFriend")
    public ResultVO respEntity(@RequestBody ReplyAddFriendDto replyAddFriendDto) {
        logger.info("replyAddFriend param" + replyAddFriendDto);
        return friendRecordService.replyAddFriend(replyAddFriendDto);
    }

    /**
     * 修改好友昵称
     *
     * @param updateFriendNicknameDto
     * @return
     */
    @ApiOperation(value = "修改好友昵称", notes = "")
    @DynamicParameters(name = "updateFriendNicknameDto", properties = {
            @DynamicParameter(name = "userId", value = "用户id", dataTypeClass = Long.class, required = true)
            , @DynamicParameter(name = "friendId", value = "好友id", dataTypeClass = Long.class, required = true)
            , @DynamicParameter(name = "friendName", value = "好友昵称", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/updateFriendNickname")
    public RespEntity updateFriendNickname(@RequestBody JSONObject updateFriendNicknameDto) {
        logger.info("updateFriendNickname param" + updateFriendNicknameDto.toString());
        Long userId = updateFriendNicknameDto.getLong("userId");
        Long friendId = updateFriendNicknameDto.getLong("friendId");
        String friendName = updateFriendNicknameDto.getString("friendName");
        Integer rest = friendRecordService.updateFriendNickname(userId, friendId, friendName);
        return new RespEntity(rest > 0 ? RespCode.SUCCESS : RespCode.FAIL);
    }

    /**
     * 根据好友id和时间查询好友档案数据(时间具体到哪一天)
     *
     * @return
     */
    @ApiOperation(value = "根据好友id和时间查询好友档案数据", notes = "时间具体到哪一天")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "friendId", value = "好友id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "date", value = "日期,yyyyMMdd", required = true, dataType = "String"),
    })
    @GetMapping("/getFriendFile")
    public RespEntity<FriendHealthFileVo> getFriendFile(@RequestParam("friendId") long friendId, @RequestParam("date") String date) {
        logger.info("getFriendFile param" + friendId, date);
        FriendHealthFileVo friendFile = friendRecordService.getFriendFile(friendId, date);
        return new RespEntity<>(RespCode.SUCCESS, friendFile);
    }


    /**
     * 删除好友
     *
     * @param friendId
     * @param userId
     * @return
     */
    @ApiOperation(value = "删除好友", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "friendId", value = "好友id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long"),
    })
    @GetMapping("/deleteFriend")
    public RespEntity deleteFriend(@Param("friendId") Long friendId, @Param("userId") Long userId) {
        logger.info("deleteFriend param" + friendId, userId);
        return friendRecordService.deleteFriend(friendId, userId);
    }

    /**
     * 分页获取历史申请
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取历史申请", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int"),
    })
    @GetMapping("/getFriendAllApplyById")
    public RespEntity getFriendAllApplyById(@RequestParam(value = "userId") String userId,@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize) {
        logger.info("getFriendAllApplyById param" + userId,pageSize,currentPage);
        return friendRequestsService.getFriendAllApplyById(userId,pageSize,currentPage);
    }
}
